package com.xuecheng.orders.listener;

import com.xuecheng.base.constant.OrderPayRecordStatusConstant;
import com.xuecheng.base.constant.RabbitMQConstant;
import com.xuecheng.orders.model.po.XcPayRecord;
import com.xuecheng.orders.service.IOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author 勾新杰
 * @version 1.0
 * @description:
 * @date 2025/3/9 16:19
 */
@Component
@RequiredArgsConstructor
public class PayRecordDelayListener {

    private final IOrderService orderService;

    /**
     * 监听支付记录延时队列
     * @param payNo 支付交易号
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = RabbitMQConstant.QUEUE_DELAY_PAY_RECORD, durable = "true"),
            exchange = @Exchange(value = RabbitMQConstant.EXCHANGE_DELAY_DIRECT),
            key = RabbitMQConstant.ROUTING_KEY_DELAY_PAY_RECORD
    ))
    public void listenPayRecordDelay(String payNo) {
        XcPayRecord payRecord = orderService.getByPayNo(payNo);
        // 查看该支付记录是不是还没支付，如果没支付，则关闭交易
        if(OrderPayRecordStatusConstant.UNPAID.equals(payRecord.getStatus()))
            orderService.closeTrade(payNo);
    }
}
